<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  components: {
    SelectGoods: async () =>
      Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectGoods()),
  },
  data() {
    return {
      appWindow: window,
      form: {},

      type: [],

      loading: true,
    };
  },
  methods: {
    SelectGoods() {
      this.$vFN.Directives.SelectGoods({
        appWindow: this.appWindow,
        data: [],
        type: "goods",
        options: ["goods"],
        onSuccess: (res) => {
          this.form.enter_goods_ids = [];
          res.data.forEach((item) => {
            if (typeof item == "string") {
              item = parseInt(item);
            }
            this.form.enter_goods_ids.push(item);
          });
        },
      });
    },
    async sum() {
      if (!await this.validate()) {
        return
      }
      this.loading = true;
      let res = await this.$vFN.Http.post(
        {
          url: "admin/setting/base",
          data: this.form,
        },
        window
      );
      if (res.success) {
        this.$vFN.Tools.messageSuccess("操作成功");
        this.getData();
      }
    },

    // 验证抽佣比例数值
    validatorNumber(rule, value, callback){

          // 0-100包含小数,限制两位
          if( parseFloat(value) > 100 ) {
             callback(new Error("最大100%"));
          }else{
               this.$vFN.Tools.validateMoney(rule, value, callback);
          }
    },
    
    // 验证 
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs["editForm"].validate((valid) => {
          valid ? resolve(true) : resolve(false)
        });
      });
    },

    async getData() {
      // 获取结算方式
      let res1 = await this.$vFN.Http.post({
        url: "admin/setting/type",
      }, window);
      this.type = res1.data.list;

      // 详情
      let res2 = await this.$vFN.Http.post({
        url: "admin/setting/detail",
      }, window);
      this.form = res2.data;
      // settle_type初始值要设为空数组
      // this.form.settle_type = [];
      
      if(typeof this.form.settle_type == "string") {
        this.form.settle_type = JSON.parse(this.form.settle_type);
      }

      this.loading = false;
    },

    // 初始化
    async init() {
      
      this.getData();
      
      
    },
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>